/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.util;

import net.cachapa.libra.data.Value;

public class Util {
	public static float Round(float Rval, int Rpl) {
		float p = (float) Math.pow(10, Rpl);
		Rval = Rval * p;
		float tmp = Math.round(Rval);
		return (float) tmp / p;
	}
	
	public static float calculateTrend(Value prevValue, Value value) {
		// Formula for calculating the trend: Tn=Tprev+Power(Wn-Tnprev)
		float trend, prevTrend, power = 0.1f;
		if (prevValue == null) {
			return value.getWeight();
		}
		prevTrend = prevValue.getTrend();
		int days = prevValue.getDate().differenceInDays(value.getDate());
		power = Math.min(days * power, 1);
		trend = prevTrend + power * (value.getWeight() - prevTrend);
		return trend;
	}
}
